<?php 
namespace Admin\Controller;
use Hdphp\Library\Controller;

/**
* 
*/
class CommonController extends Controller
{
	public function __construct(){
		parent::__construct();

		//自定义加入判断 缓存配置文件 和 目录
		$filename=C('DEFAULT_DB');
		$dirname=dirname($filename);
		is_dir($dirname) || mkdir($dirname,0777,true);
		file_exists($filename) || fopen($filename, "w");

		if(!isset($_SESSION['admin_uname']) || !isset($_SESSION['admin_aid'])){
			$this->error('请登录','./index.php?m=admin&c=login');
		}
	}


	/**
	 * 分类数据序列化缓存
	 * $data 缓存的数据
	 * $path 缓存的路径
	 * $time 过期时间
	 */
	protected function cateDataWrite($data,$path=null,$time=null){
		//加入时间判断超时
		// p($path);die;
		$path=is_null($path) ? C('DEFAULT_DB') : $path ;	
		$time=is_null($time) ? C('DEFAULT_time'): intval($time);
		$data['expire']=time()+$time;
		$data=serialize($data);
/*		$str=<<<str
<?php
return {$data};
?>
str;*/
		file_put_contents($path, $data);

	}


	/**
	 * 分类数据读取
	 * $path 缓存文件路径
	 */
	protected function cateDataRead($path=null){
		$path=is_null($path) ?  C('DEFAULT_DB') :$path ;	
		$data=unserialize(file_get_contents($path)) ;
		return $data;
	}


	//面包屑导航
	protected function Breadcrumbs($cid){
		//处理面包屑导航
		$cateData =M()->query("SELECT * FROM hd_category");
		$parentsCate = $this->_getParents($cateData,$cid);
		//数组反转
		$parentsCate = array_reverse($parentsCate);
		$this->assign('parentsCate',$parentsCate);
	}


	//通过子类id找父级  面包屑调用
	protected function _getParents($data,$cid){
		$temp=array();
		foreach ($data as $key => $value) {
			if($value['cid']==$cid){
				$temp[]=$value;
				$temp=array_merge($temp,$this->_getParents($data,$value['pid']));
			}
		}
		return $temp;
	}

	// 读取分类信息
/*	protected function cateData(){
		//读取缓存
		$data=$this->cateDataRead();
		//判断过期
		if(time()>$data['expire']){
			$sql="SELECT * FROM hd_category";
			$category=M()->query($sql);
			//写入缓存
			$this->cateDataWrite($category);
		}
		unset($data['expire']);
		return $data;
	}
*/


}





 ?>